package com.cswangdao.service.impl;

import com.cswangdao.domain.dto.UserPageDTO;
import com.cswangdao.domain.model.MarketComment;
import com.cswangdao.domain.po.User;
import com.cswangdao.domain.vo.AdminInfo;
import com.cswangdao.domain.vo.UserInfo;
import com.cswangdao.domain.vo.WXOrderStatusNumVo;
import com.cswangdao.mapper.MarketCommentMapper;
import com.cswangdao.mapper.MarketOrderMapper;
import com.cswangdao.mapper.MarketUserMapper;
import com.cswangdao.result.PageResult;
import com.cswangdao.service.UserService;
import com.cswangdao.utils.SubjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private MarketUserMapper userMapper;

    @Autowired
    private MarketOrderMapper orderMapper;

    @Autowired
    private MarketCommentMapper commentMapper;

    @Override
    public PageResult<User> pageQueryInfo(UserPageDTO userPageDTO) {

        try/*(SqlSession sqlSession = MybatisUtil.getSqlSession(true)) */{
            PageHelper.startPage(userPageDTO.getPage(),userPageDTO.getLimit());
         //   MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);

            Page<User> userPage = userMapper.pageQueryInfo(userPageDTO);
            PageResult<User> result = PageResult.result(userPage);
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User getUserById(Integer id) {
        User user = userMapper.getUserById(id);
        return user;
    }

    @Override
    public Map<String, Object> login(String username, String password) {

        //生成token
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //获取subject对象
        Subject subject = SecurityUtils.getSubject();
        //调用login方法进行登录
        subject.login(token);
        //判断是否登录成功
        if(!subject.isAuthenticated()){
        //失败，返回错误信息
            throw new RuntimeException("登录失败");
        }
        //成功，返回token和用户信息
        Map<String, Object> map = new HashMap<>();
        //获取用户id
        Integer userId = (Integer) subject.getPrincipal();
        //根据用户id获取用户信息
        User user = userMapper.getUserById(userId);
        UserInfo userInfo = UserInfo.builder()
                .avatarUrl(user.getAvatar())
                .nickName(user.getNickname())
                .build();
        //获取token=sessionId
        map.put("token",subject.getSession().getId());
        map.put("userInfo",userInfo);
        return map;

    }

    @Override
    public User updateById(User user) {
        userMapper.updateById(user);
        return user;
    }

    @Override
    public  Map<String, WXOrderStatusNumVo>  index() {
        Integer userId = SubjectUtil.getUserId();
       // List<MarketComment> comments = commentMapper.getByUserId(userId);
        Integer unpaid = orderMapper.getOrdersByStatusAndUserId(userId,101);
        Integer unship = orderMapper.getOrdersByStatusAndUserId(userId,201);
        Integer unrecv = orderMapper.getOrdersByStatusAndUserId(userId,301);
        Integer uncomment = orderMapper.getOrdersByStatusAndUserId(userId,401);
        WXOrderStatusNumVo wxOrderStatusNumVo = WXOrderStatusNumVo.builder()
                .unpaid(unpaid)
                .unship(unship)
                .unrecv(unrecv)
                .uncomment(uncomment)
                .build();
        Map<String, WXOrderStatusNumVo> map = new HashMap<>();
        map.put("order", wxOrderStatusNumVo);
        return map;
    }



}
